package com.sdry.service.impl.cb;

import com.github.pagehelper.PageInfo;
import com.sdry.mapper.cb.ReportFormMapper;
import com.sdry.model.cb.ReportFormEntity;
import com.sdry.model.cb.TableDataInfo;
import com.sdry.model.jyy.Receive;
import com.sdry.model.llm.LlmCancellingStocks;
import com.sdry.model.lz.Customer;
import com.sdry.model.lz.Materiel;
import com.sdry.model.lz.OutStockOrder;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.model.zc.ZcRejectsWarehouseEntity;
import com.sdry.service.cb.ReportFormService;
import com.sdry.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报表
 */
@Service
@Transactional(rollbackFor = Exception.class)
    public class ReportFormServiceImpl implements ReportFormService {

    @Autowired
    private ReportFormMapper reportFormMapper;

    @Override
    public TableDataInfo getReportFormList(String ma_id, String date, Long customer_id, String firsTime, String selectTime2) throws ParseException {
        String RS = "monthReport";
        return computationalBalance(date,ma_id,customer_id,firsTime,RS,selectTime2);
    }

    @Override
    public List<Receive> getReportFormJinList(String selectTime, String maName, String date, Long customer_id, String selectType, String selectTime2) {

        if ("2".equals(selectTime) || "3".equals(selectTime)) {
            date = selectTime2 = null;
        }

        if (StringUtil.isEmpty(date)) {
            date = null;
        }if ("3".equals(date)) {
            date = null;
        }


        if (StringUtil.isEmpty(maName)){
            maName = null;
        }

         return reportFormMapper.getReportFormJinList(date,maName,customer_id,selectTime,selectType,selectTime2);
    }


    @Override
    public List<OutStockOrder> getReportFormChungList(String maName, String date, Long customer_id, String selectTime, String selectType, String selectTime2) {
        if ("2".equals(selectTime) || "3".equals(selectTime)) {
            date = selectTime2 = null;
        }
        if (StringUtil.isEmpty(date)) {
            date = null;
        }if ("3".equals(date)) {
            date = null;
        }
        if (StringUtil.isEmpty(maName)){
            maName = null;
        }

        return reportFormMapper.getReportFormChuList(date,maName,customer_id,selectTime,selectType,selectTime2);
    }
    @Override
    public String getReportFormFirsTime() {
        return reportFormMapper.getReportFormFirsTime();
    }
    @Override
    public List<LlmCancellingStocks> getReportFormReTurnList(String selectTime, String maName, String date, Long customer_id, String selectType, String dateTime2) {
        if (StringUtil.isEmpty(date)) {
            date = null;
        }
        if (StringUtil.isEmpty(maName)){
            maName = null;
        }
        return reportFormMapper.getReportFormReTurnList(date,maName,customer_id,selectTime,selectType,dateTime2);
    }

    @Override
    public List<ZcRejectsWarehouseEntity> getReportFormReGeLIList(String selectTime, String maName, String date, Long customer_id, String selectType, String selectTime2) {

        if ("2".equals(selectTime) || "3".equals(selectTime)) {
            date = selectTime2 = null;
        }

        if (StringUtil.isEmpty(date)) {
            date = null;
        }if ("3".equals(date)) {
            date = null;
        }

        if (StringUtil.isEmpty(maName)){
            maName = null;
        }

        return reportFormMapper.getReportFormReGeLIList(date,maName,customer_id,selectTime,selectType,selectTime2);
    }


    @Override
    public TableDataInfo getDateBalance(String ma_id, String date, Long customer_id, String firsTime, String dateTime2) throws ParseException {
        String RS = "dateReport";
        return computationalBalance(date,ma_id,customer_id,firsTime,RS,dateTime2);
    }
    @Override
    public List<Receive> getReceiveDetail(String dateTime) {
        List<Receive> list = reportFormMapper.getReportFormJinList(dateTime,null,null,null,"5", null);


        return list;
    }

    @Override
    public List<ZcInventoryInfoEntity> getReportInventoryList(Long customer_id, String maName, String date) {
        return reportFormMapper.getReportInventoryList(customer_id,maName,date);
    }

    @Override
    public List<Materiel> getInventoryInfoList(String mid) {
        return reportFormMapper.getInventoryInfoList(mid);
    }

    @Override
    public List<Materiel> getStockInventoryInfoDetailedList(String mid, String mBatch, Integer pageNum, Integer limitNum) {
        Integer limit = pageNum * limitNum;
        Integer page = (pageNum - 1) * limitNum;
        return reportFormMapper.getStockInventoryInfoDetailedList(mid,mBatch,page,limit);
    }

    @Override
    public Integer getReportFormJinListTotal(String selectTime, String maName, String date, Long customer_id, String selectType, String selectTime2) {

        if ("2".equals(selectTime) || "3".equals(selectTime)) {
            date = selectTime2 = null;
        }
        if (StringUtil.isEmpty(date)) {
            date = null;
        }if ("3".equals(date)) {
            date = null;
        }
        if (StringUtil.isEmpty(maName)){
            maName = null;
        }
        return reportFormMapper.getReportFormJinListTotal(date,maName,customer_id,selectTime,selectType,selectTime2);
    }

    @Override
    public Integer getReportFormChungListTotal(String maName, String date, Long customer_id, String selectTime, String selectType, String selectTime2) {
        if ("2".equals(selectTime) || "3".equals(selectTime)) {
            date = selectTime2 = null;
        }
        if (StringUtil.isEmpty(date)) {
            date = null;
        }if ("3".equals(date)) {
            date = null;
        }

        if (StringUtil.isEmpty(maName)){
            maName = null;
        }
        return reportFormMapper.getReportFormChungListTotal(date,maName,customer_id,selectTime,selectType,selectTime2);
    }

    @Override
    public Integer getReportFormReTurnListTotal(String selectTime, String maName, String date, Long customer_id, String selectType, String dateTime2) {
        if ("2".equals(selectTime) || "3".equals(selectTime)) {
            date = dateTime2 = null;
        }
        if (StringUtil.isEmpty(date)) {
            date = null;
        }if ("3".equals(date)) {
            date = null;
        }
        if (StringUtil.isEmpty(maName)){
            maName = null;
        }
        return reportFormMapper.getReportFormReTurnListTotal(date,maName,customer_id,selectTime,selectType,dateTime2);
    }

    @Override
    public Integer getReportFormReGeLIListTotal(String selectTime, String maName, String date, Long customer_id, String selectType, String selectTime2) {
        if ("2".equals(selectTime) || "3".equals(selectTime)) {
            date = selectTime2 = null;
        }
        if (StringUtil.isEmpty(date)) {
            date = null;
        }if ("3".equals(date)) {
            date = null;
        }
        if (StringUtil.isEmpty(maName)){
            maName = null;
        }
        return reportFormMapper.getReportFormReGeLIListTotal(date,maName,customer_id,selectTime,selectType,selectTime2);
    }

    @Override
    public List<Customer> selectAllCustomerList() {

        return reportFormMapper.selectAllCustomerList();
    }

    @Override
    public List<Materiel> getStockInventoryInfoDetailedListTotal(String mid, String mBatch) {
        return reportFormMapper.getStockInventoryInfoDetailedListTotal(mid,mBatch);
    }

    @Override
    public List<Materiel> getTiaoMaList(String mBatch, String mid, String locationCode) {
        return reportFormMapper.getTiaoMaList(mBatch, mid, locationCode);
    }


    /**
     * 报表
     * @param date 查询日期
     * @param selectTime2
     * @return
     * @throws ParseException
     */
    public TableDataInfo computationalBalance(String date, String ma_id, Long customer_id, String firsData, String RS, String selectTime2) throws ParseException {
        SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");

        if (StringUtil.isEmpty(date)) {
            Date date1 = new Date();
            date = sf1.format(date1);
        }

        if (StringUtil.isNotEmpty(date)) {
            date = date.substring(0, 10);
            selectTime2 = selectTime2.substring(0, 10);
        }

        if (StringUtil.isEmpty(ma_id)){
            ma_id = null;
        }
        Date toDate = sf1.parse(date);
        //1. 第一个月入库记录从什么时间开始
        String firsTime = firsData;
        //2.记录上个月的所有记录
        List<ReportFormEntity> list2 = new ArrayList<>();
        Integer total2 = 0;
        Integer compareMonth = dayCompare(sf1.parse(firsTime), toDate);
        for (int i = 0; i <= compareMonth; i++) {
            List<ReportFormEntity> list = new ArrayList<>();
            Integer total = 0;
            if ("monthReport".equals(RS)){
                list = reportFormMapper.getMonthBalance(sf1.parse(firsTime),ma_id,customer_id,selectTime2,date);
                total = reportFormMapper.getMonthBalanceTotal(sf1.parse(firsTime),ma_id,customer_id,selectTime2,date);
            }else if ("dateReport".equals(RS)){
                list = reportFormMapper.getDateBalance(sf1.parse(firsTime),ma_id,customer_id);
            }
            Integer lastMonthBalance = 0;
            for (int j = 0; j < list.size(); j++) {
                if (i != 0) {
                    lastMonthBalance  = list2.get(j-1).getLastMonthBalance();
                }
                list.get(j).setThisMonthBalance(list.get(j).getThisMonth()+lastMonthBalance);
                list.get(j).setLastMonthBalance(lastMonthBalance);
            }
            list2.clear();
            list2.addAll(list);
            total2 = total;
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(sf1.parse(firsTime));
            rightNow.add(Calendar.MONTH, 1);
            firsTime = sf1.format(rightNow.getTime());
        }
        //return list2;
        PageInfo pageInfo = new PageInfo(list2,5);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setRows(pageInfo.getList());
        rspData.setTotal(total2);
        rspData.setCode(0);
        return rspData;
    }

    /**
     * 计算两个日期相差月份
     * @param fromDate
     * @param toDate
     * @return
     */
    public static Integer dayCompare(Date fromDate,Date toDate) {
        Calendar from = Calendar.getInstance();
        Calendar to = Calendar.getInstance();
        from.setTime(fromDate);
        to.setTime(toDate);
        Integer month =  to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
        return month;
    }

}
